// @ts-nocheck
var express = require("express");
var router = express.Router();
const fs = require("fs/promises");
const path = require("path");
// 在global上挂载一个map，用于保存各个文件的信息
global.keyMap = new Map();
router.post("/", async function (req, res) {
	const { key, ext, length } = req.body;

	const fileName = `${key}.${ext}`;
	const filePath = path.resolve(__dirname, `../public/file/${fileName}`);

	await fs.writeFile(filePath, "");
	const fd = await fs.open(filePath, "r+");
	const value = {
		fd,
		hasResolve: new Array(length).fill(0),
	};
	keyMap.set(key, value);
	res.send(
		JSON.stringify({
			code: 200,
			data: "ok",
		})
	);
});

module.exports = router;
